package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public List<DeliveryAddress> selectByUserId(String userId) throws SQLException {
        List<DeliveryAddress> list = new ArrayList<>();
        String sql = "select * from deliveryaddress d where userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                DeliveryAddress address = new DeliveryAddress();
                address.setDaId(rs.getInt("daId"));
                address.setContactName(rs.getString("contactName"));
                address.setContactSex(rs.getInt("contactSex"));
                address.setContactTel(rs.getString("contactTel"));
                address.setAddress(rs.getString("address"));
                address.setUserId(rs.getString("userId"));
                list.add(address);
            }
        } finally {
            DBUtil.close(pst);
        }
        return list;
    }

    @Override
    public DeliveryAddress selectById(Integer daId) throws SQLException {
        DeliveryAddress address = null;
        String sql = "select * from deliveryaddress d where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            rs = pst.executeQuery();
            while (rs.next()) {
                address = new DeliveryAddress();
                address.setDaId(rs.getInt("daId"));
                address.setContactName(rs.getString("contactName"));
                address.setContactSex(rs.getInt("contactSex"));
                address.setContactTel(rs.getString("contactTel"));
                address.setAddress(rs.getString("address"));
                address.setUserId(rs.getString("userId"));
            }
        } finally {
            DBUtil.close(pst);
        }

        return address;
    }

    @Override
    public Integer saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) throws SQLException {
        int result = 0;
        String sql = "insert into deliveryaddress(contactName,contactSex,contactTel,address,userId) values(?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, contactName);
            pst.setInt(2, contactSex);
            pst.setString(3, contactTel);
            pst.setString(4, address);
            pst.setString(5, userId);
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Integer updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) throws SQLException {
        int result = 0;
        String sql = "update deliveryaddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, contactName);
            pst.setInt(2, contactSex);
            pst.setString(3, contactTel);
            pst.setString(4, address);
            pst.setString(5, userId);
            pst.setInt(6, daId);
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Integer removeDeliveryAddress(Integer daId) throws SQLException {
        int result = 0;
        String sql = "delete from deliveryaddress where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }
}
